﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;

namespace ChangeHCAppconfig
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length==2)
            {
                string conf = args[0];
                string ver = args[1];

                //string conf = @"App.config";
                //string ver = @"8.0.0.0";

                if (File.Exists(conf))
                {
                    Console.WriteLine("File found: " + conf);
                    XmlDocument xml = new XmlDocument();
                    xml.Load(conf);

                    XmlNodeList modules = xml.GetElementsByTagName("modules");
                    if (modules != null)
                    {
                        foreach (XmlElement e in modules[0].ChildNodes)
                        {
                            string moduleType = e.Attributes["moduleType"].InnerText;
                            e.Attributes["moduleType"].InnerText = ConvertVersion(moduleType, ver);
                        }
                    }
                    xml.Save(conf);
                }
                else
                    Console.WriteLine("File not found: " + conf);
            }
        }

        private static string ConvertVersion(string str, string version)
        {
            string pattern = @"Version=\d\.\d\.\d\.\d";
            Regex rgx = new Regex(pattern);
            str = rgx.Replace(str, "Version=" + version);
            return str;
        }
    }
}
